package com.asj.service.request;

import static com.asj.enums.DeviceIdentifierDictEnums.json_key_deviceCode;
import static com.asj.enums.HttpRequestDictEnums.queryDeviceExistUrl;
import static com.asj.enums.ResultCodeEnums.request_error;
import static com.asj.enums.ResultCodeEnums.request_success;

import com.asj.utils.HttpRequestUtils;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;


/**
 * 发送请求服务
 * */
public class RequestMDMService {

    public static JsonObject queryExistDevice(String deviceCode) throws Exception {
        JsonObject deviceExistRequestParam = new JsonObject();
        deviceExistRequestParam.addProperty(json_key_deviceCode.getValue(), deviceCode);
        String resultData = HttpRequestUtils.sendJsonPostRequest(queryDeviceExistUrl.getValue(), deviceExistRequestParam.toString());
        if(resultData.isEmpty()){
            return null;
        }
        return JsonParser.parseString(resultData).getAsJsonObject();
    }



    public static String sendResult(String data, String methodUrl) throws IOException {
        String resultData = HttpRequestUtils.sendJsonPostRequest(methodUrl, data);
        //默认不存在
        if(!resultData.isEmpty() && JsonParser.parseString(resultData).getAsJsonObject().get("code").getAsInt() == request_success.getCode()){
            System.out.println(request_success.getMessage());
        }else{
            System.out.println(request_error.getMessage());
        }
        return resultData;
    }
}
